package com.example.springboot_07_filter.fillter;

import com.example.springboot_07_filter.po.DemoBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Order(2)//标注过滤器优先级
@Slf4j
@WebFilter
public class AuthFilter implements Filter {
    private DemoBean bean;
    public AuthFilter() {
        System.out.println("init autFilter");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("过滤器in auth filter!{}", bean);
        bean.show();
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String auth= request.getHeader("tx-demo");
        if("yihuihui".equals(auth)) {
            filterChain.doFilter(servletRequest, servletResponse);
        }else{
            filterChain.doFilter(servletRequest, servletResponse);
        }

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
